﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// Summary description for News_session
/// </summary>
public class News_session
{
    private static SqlConnection sqlConnection;

    private DataTable objDataTable;
    public static SqlDataAdapter sqlAdapter;
    public static SqlCommand sqlCommand; 
	public News_session()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static List<News> GetList_News()
    {
        DataTable dt = DataProvider.GetDataTable("select * from News");
        List<News> lst = new List<News>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            News obj = new News();

            obj.News_ID = (dt.Rows[i]["News_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["News_ID"];
            obj.News_Status = (dt.Rows[i]["News_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[i]["News_Status"];
            obj.News_Title = (dt.Rows[i]["News_Title"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["News_Title"];
            obj.News_Content = (dt.Rows[i]["News_Content"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["News_Content"];
            obj.News_Image = (dt.Rows[i]["News_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["News_Image"];
            obj.News_Hit = (dt.Rows[i]["News_Hit"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[i]["News_Hit"];
            obj.Cate_ID = (dt.Rows[i]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Cate_ID"];
            obj.News_Intro = (dt.Rows[i]["News_Intro"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["News_Intro"];
            obj.News_CreateDate = (dt.Rows[i]["News_CreateDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[i]["News_CreateDate"];
            obj.News_Source = (dt.Rows[i]["News_Source"] is DBNull) ? string.Empty : (System.String)dt.Rows[i]["News_Source"];
            lst.Add(obj);
        }
        return lst;
    }
    public static DataTable Get_NewsAsTable(String swhere, String sorder)
    {
        DataTable dt = new DataTable(); // chu y cho nay nek
        dt = DataProvider.GetDataTable("select * from News " + swhere + sorder);
        return dt;
    }
    public static News Get_News_ByID(System.Int32 News_ID)
    {
        DataTable dt = new DataTable();
        dt = DataProvider.GetDataTable("select * from News where News_ID = " + News_ID);
        News obj = new News();

        obj.News_ID = (dt.Rows[0]["News_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["News_ID"];
        obj.News_Status = (dt.Rows[0]["News_Status"] is DBNull) ? false : (System.Boolean)dt.Rows[0]["News_Status"];
        obj.News_Title = (dt.Rows[0]["News_Title"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["News_Title"];
        obj.News_Content = (dt.Rows[0]["News_Content"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["News_Content"];
        obj.News_Image = (dt.Rows[0]["News_Image"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["News_Image"];
        obj.News_Hit = (dt.Rows[0]["News_Hit"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["News_Hit"];
        obj.Cate_ID = (dt.Rows[0]["Cate_ID"] is DBNull) ? int.MinValue : (System.Int32)dt.Rows[0]["Cate_ID"];
        obj.News_Intro = (dt.Rows[0]["News_Intro"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["News_Intro"];
        obj.News_CreateDate = (dt.Rows[0]["News_CreateDate"] is DBNull) ? DateTime.MinValue : (System.DateTime)dt.Rows[0]["News_CreateDate"];
        obj.News_Source = (dt.Rows[0]["News_Source"] is DBNull) ? string.Empty : (System.String)dt.Rows[0]["News_Source"];
        
        return obj;
    }
    public static void Delete_News(News temp)
    {
        String strSQL;
        
        strSQL = "delete  from News where News_ID = " + temp.News_ID;
        sqlConnection = new SqlConnection();
        sqlConnection = DataProvider.CreateConnection();
        SqlCommand cmd = new SqlCommand(strSQL, sqlConnection);
        cmd.CommandType = CommandType.Text; ;
        cmd.ExecuteNonQuery();

    }
    public static void Update_News(News temp)
    {
        sqlConnection = new SqlConnection();
        sqlConnection = DataProvider.CreateConnection();


        String strSQL;

        strSQL = "UPDATE News  SET  News_Status = @News_Status , News_Title = @News_Title,News_Content = @News_Content, News_Image= @News_Image, News_Hit = @News_Hit, Cate_ID = @Cate_ID,News_Intro = @News_Intro , News_CreateDate = @News_CreateDate , News_Source = @News_Source  WHERE News_ID= @News_ID ";
        SqlCommand sqlCommand = new SqlCommand(strSQL, sqlConnection);
        sqlCommand.CommandType = CommandType.Text;

        sqlCommand.CommandText = strSQL;
        sqlCommand.Parameters.Add("@News_ID", SqlDbType.Int).Value = temp.News_ID;
        sqlCommand.Parameters.Add("@News_Status", SqlDbType.Bit).Value = temp.News_Status;
        sqlCommand.Parameters.Add("@News_Title", SqlDbType.NVarChar).Value = temp.News_Title;
        sqlCommand.Parameters.Add("@News_Content", SqlDbType.NText).Value = temp.News_Content;
        sqlCommand.Parameters.Add("@News_Image", SqlDbType.VarChar).Value = temp.News_Image;
        sqlCommand.Parameters.Add("@News_Hit", SqlDbType.Int).Value = temp.News_Hit;
        sqlCommand.Parameters.Add("@Cate_ID", SqlDbType.Int).Value = temp.Cate_ID;
        sqlCommand.Parameters.Add("@News_Intro", SqlDbType.NVarChar).Value = temp.News_Intro;
        sqlCommand.Parameters.Add("@News_CreateDate", SqlDbType.DateTime).Value = temp.News_CreateDate;
        sqlCommand.Parameters.Add("@News_Source", SqlDbType.NVarChar).Value = temp.News_Source;

        sqlCommand.ExecuteNonQuery();// tai sao bi bao loi
        sqlCommand.Dispose();
        sqlCommand = null;
    }

}
